package com.cwd.arm.inter.controller;

import com.cwd.arm.application.dto.ConfirmIncomeDto;
import com.cwd.arm.application.service.read.facade.ConfirmIncomeReadService;
import com.cwd.arm.application.service.write.facade.ConfirmIncomeWriteService;
import com.cwd.arm.application.service.write.facade.TargetConfirmAmountWriteService;
import com.cwd.arm.inter.query.ConfirmIncomeQuery;
import com.cwd.arm.inter.util.DtoToVoUtil;
import com.cwd.arm.inter.util.VoToDtoUtil;
import com.cwd.arm.inter.vo.ResponseVo;
import com.cwd.arm.inter.vo.confirmIncome.ConfirmIncomeAddVo;
import com.cwd.arm.inter.vo.confirmIncome.ConfirmIncomeUpdateVo;
import com.cwd.arm.inter.vo.confirmIncome.ConfirmIncomeVo;
import com.cwd.arm.inter.vo.confirmIncome.TargetConfirmAmountUpdateVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * Description:确认收入
 * Created on 2021年11月16日
 *
 * @author yan
 */
@Api(value = "确认收入管理接口", description = "确认收入管理接口")
@RestController
@RequestMapping(value = "/confirmIncome")
public class ConfirmIncomeController {

    @Autowired
    private ConfirmIncomeReadService readService;

    @Autowired
    private ConfirmIncomeWriteService writeService;

    @Autowired
    private TargetConfirmAmountWriteService targetConfirmAmountWriteService;

    @ApiOperation(value = "查询确认收入信息", notes = "查询确认收入信息")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id", value = "确认收入id", required = true, dataType = "Long", paramType = "path")
    })
    @GetMapping("/{id}")
    public ResponseVo<ConfirmIncomeVo> getConfirmIncome(@PathVariable Long id) {
        ConfirmIncomeDto dto = readService.selectById(id);
        return ResponseVo.<ConfirmIncomeVo>BUILDER().success(VoToDtoUtil.convertToVo(dto));
    }

    @ApiOperation(value = "查询确认收入列表信息", notes = "查询确认收入列表信息")
    @GetMapping("")
    public ResponseVo<List<ConfirmIncomeVo>> getConfirmIncomeList(ConfirmIncomeQuery projectQuery) {
        List<ConfirmIncomeDto> dtoList = readService.selectByProjectId(projectQuery.getProjectId());
        return ResponseVo.<List<ConfirmIncomeVo>>BUILDER().success(DtoToVoUtil.convertToConfirmIncomeVoList(dtoList));
    }

    @ApiOperation(value = "新增确认收入", notes = "新增确认收入")
    @Secured({"ROLE_CONFIRMINCOME_ADD"})
    @PostMapping("")
    public ResponseVo<String> add(@RequestBody ConfirmIncomeAddVo addVo) {
        writeService.save(VoToDtoUtil.convertToDto(addVo));
        return ResponseVo.<String>BUILDER().success();
    }

    @ApiOperation(value = "修改确认收入", notes = "修改确认收入")
    @Secured({"ROLE_CONFIRMINCOME_EDIT"})
    @PutMapping("")
    public ResponseVo<String> update(@RequestBody ConfirmIncomeUpdateVo updateVo) {
        writeService.update(VoToDtoUtil.convertToDto(updateVo));
        return ResponseVo.<String>BUILDER().success();
    }

    @ApiOperation(value = "删除确认收入", notes = "删除收确认收入")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id", value = "确认收入id", required = true, dataType = "Long", paramType = "path")
    })
    @Secured({"ROLE_CONFIRMINCOME_DEL"})
    @DeleteMapping("/{id}")
    public ResponseVo<String> del(@PathVariable Long id) {
        writeService.del(id);
        return ResponseVo.<String>BUILDER().success();
    }

    @ApiOperation(value = "确认收入百分比金额转换", notes = "根据金额得到百分比,根据百分比得到金额")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "projectId", value = "项目id", required = true, dataType = "Long")
            ,@ApiImplicitParam(name = "amount", value = "确认金额", required = true, dataType = "BigDecimal")
            ,@ApiImplicitParam(name = "confirmIncomeId", value = "确认收入id(修改时必传)", dataType = "BigDecimal")
            ,@ApiImplicitParam(name = "date", value = "确认时间(格式:2022-01-01)", required = true, dataType = "date")
    })
    @GetMapping("/rate")
    public ResponseVo<BigDecimal> getRate(Long projectId, BigDecimal amount, Long confirmIncomeId, String date) throws Exception{
        return ResponseVo.<BigDecimal>BUILDER().success(readService.getRate(projectId, amount, confirmIncomeId, date));
    }

    @ApiOperation(value = "收款节点百分比转金额", notes = "收款节点百分比转金额")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "projectId", value = "项目id", required = true, dataType = "Long")
            ,@ApiImplicitParam(name = "rate", value = "比例", required = true, dataType = "BigDecimal")
            ,@ApiImplicitParam(name = "confirmIncomeId", value = "确认收入id(修改时必传)", dataType = "BigDecimal")
            ,@ApiImplicitParam(name = "date", value = "确认时间(格式:2022-01-01,修改时必传)", required = true, dataType = "date")
    })
    @GetMapping("/amount")
    public ResponseVo<BigDecimal> getAmount(Long projectId, BigDecimal rate, Long confirmIncomeId, String date) throws Exception {
        return ResponseVo.<BigDecimal>BUILDER().success(readService.getAmount(projectId, rate, confirmIncomeId, date));
    }

    @ApiOperation(value = "保存确认收入目标", notes = "保存确认收入目标:根据projectId和year有就修改，没有就新增")
    @Secured({"ROLE_CONFIRMINCOME_TAR_SAVE"})
    @PostMapping("/tar")
    public ResponseVo<String> updateTargetConfirmAmount(@RequestBody TargetConfirmAmountUpdateVo updateVo) {
        targetConfirmAmountWriteService.saveOrUpdate(VoToDtoUtil.convertToDto(updateVo));
        return ResponseVo.<String>BUILDER().success();
    }

}
